<?php
use yii\bootstrap\ActiveForm;
?>
<link rel="stylesheet" href="../static/dist/css/AdminLTE.min.css" type="text/css">
<link rel="stylesheet" href="../static/bootstrap/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/datatables/jquery.dataTables.css" type="text/css">
<link rel="stylesheet" href="../static/css/admin.css" type="text/css">
<link rel="stylesheet" href="../static/jBox/Skins/Metro/jbox.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/datepicker/datepicker3.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/daterangepicker/daterangepicker.css" type="text/css">
<style>
    .dataTables_length {
        margin-top: 10px;
    }
    .dataTable>tbody>tr>td {
        vertical-align: middle;
        font-size: 0.8em;
        padding: 8px 18px;
    }
    .dataTable>thead>tr>th {
        vertical-align: middle;
        font-size: 0.8em;
    }
    .source_type_class {
        min-height: 20px;
        padding-left: 20px;
        margin-bottom: 0;
        font-weight: 400;
        cursor: pointer;
    }
    .form-left{
        border-right: 1px solid #dddddd;
    }
    .examine-content{
        width: 1000px;
        margin-left:-300px;
    }
    .examine-content .input-group .form-control{
        margin-bottom: 20px;
        height: 30px ;
        line-height: 30px;
    }
    .examine-content button{
        margin: 10px;
    }
    #table_id_example th, td {
        white-space: nowrap;
    }
</style>
<div class="tabs-panel">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <div style="margin-bottom: 5px">
                        订单编号：<input class="depart-input" id="orderNo" style="width: 160px" type="text">&nbsp;
                        仓库：<select id="warehouseId" name="warehouseId" class="depart-input" style="width: 200px">
                            <option value="">选择仓库</option>
                            <?php foreach($warehouse as $value) { ?>
                                <option value="<?= $value['id']?>"><?= $value['warehouseName']?></option>
                            <?php }?>
                        </select>&nbsp;
                        采购时间：<input class="depart-input" id="purchaseDate" style="width: 200px" type="text">&nbsp;
                        <button type="button" class="btn btn-default" style="width: 50px;" onclick="$('#purchaseDate').val('')">清空</button>&nbsp;
                        订单状态：
                        <select id="orderStatus" class="depart-input" style="width: 160px">
                            <option value="">全部</option>
                            <option value="0">创建</option>
                            <option value="1">审核</option>
                            <option value="2">取消</option>
                            <option value="9">关闭</option>
                        </select>
                        <button type="button" class="btn btn-primary depart-modal" id="select_button" style="width: 80px; margin-left: 41px">查询</button>&nbsp;
                        <button type="button" class="btn btn-warning depart-modal" id="reset_button" style="width: 80px;">重置</button>
                    </div>
                </div>
                <div class="box-body">
                    <a type="button" class="btn btn-success depart-modal" href="/purchase-order/detail" style="width: 80px;">添加</a>
                    <table id="table_id_example" class="display">
                        <thead>
                        <tr>
                            <th style="width: 5%">操作</th>
                            <th style="width: 6%">订单编号</th>
                            <th style="width: 5%">状态</th>
                            <th style="width: 10%">供应商</th>
                            <th style="width: 12%">采购商抬头名称</th>
                            <th style="width: 10%">仓库</th>
                            <th style="width: 8%">采购日期</th>
                            <th style="width: 10%">预计送货日期</th>
                            <th style="width: 5%">下单人</th>
                            <th style="width: 10%">下单时间</th>
                            <th style="width: 5%">审核人</th>
                            <th style="width: 10%">审核时间</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 固定js -->
<script type="text/javascript" src="../static/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script type="text/javascript" src="../static/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../static/plugins/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../static/plugins/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="../static/jBox/jquery.jBox-2.3.min.js"></script>
<script type="text/javascript" src="../static/jBox/i18n/jquery.jBox-zh-CN.js"></script>
<!-- 自定义js -->
<script type="text/javascript" src="../static/plugins/moment/moment.min.js"></script>
<script type="text/javascript" src="../static/plugins/daterangepicker/daterangepicker.js"></script>
<script type="text/javascript" src="../static/plugins/datepicker/bootstrap-datepicker.js"></script>
<script type="text/javascript" src="/static/app/global.js?version=<?=Yii::$app->params['version']?>"></script>
<script>
    var table = null;
    $(function () {

        $('#purchaseDate').daterangepicker({
            language: 'zh-CN',
            timePicker: false,
            timePickerIncrement: 30,
            format: 'YYYY-MM-DD',
            locale: {
                format: 'YYYY-MM-DD',
                daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
                monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
            }
        });
        $('#purchaseDate').val('');

        table = $('#table_id_example').DataTable({
            dom: '<"top">rt<"bottom"flip><"clear">',
            language: {
                "sUrl":'../static/plugins/datatables/language.json'
            },
            "searching": false,
            "processing": true,
            "serverSide": true,
            "scrollY": "550px",
            "scrollCollapse": true,
            "scrollX": true,
            ajax:{
                url:'/purchase-order/get-list',
                type:'post',
                data:function(d){
                    d.orderNo = $('#orderNo').val();
                    d.warehouseId = $('#warehouseId').val();
                    d.purchaseDate = $('#purchaseDate').val();
                    d.orderStatus = $('#orderStatus').val();
                },
                error:getError
            },
            aoColumns: [
                { "data": "", "bSortable": false},
                { "data": "orderNo", "bSortable": false },
                { "data": "status", "bSortable": false },
                { "data": "supplierName", "bSortable":false },
                { "data": "purchaserName", "bSortable":false },
                { "data": "warehouseName", "bSortable":false },
                { "data": "purchaseDate", "bSortable":false },
                { "data": "pre_deliveryDate", "bSortable":false },
                { "data": "creator", "bSortable":false },
                { "data": "create_time", "bSortable":false },
                { "data": "auditor", "bSortable":false },
                { "data": "audit_time", "bSortable":false },
            ],
            columnDefs: [
                {
                    "targets": [0],
                    "orderable":false,
                    "searchable":false,
                    "render": function(data, type, full) {
                        var html = '';
                        if(full.status == '0') {
                            html += '<a href="javascript:audit(\''+full.orderNo+'\')">审核</a>';
                            html += '&nbsp;<a href="javascript:editStatus(\''+full.orderNo+'\',2)">取消</a>';
                        }
                        if(full.status == '1') {
                            html += '&nbsp;<a href="javascript:editStatus(\''+full.orderNo+'\',9)">关闭</a>';
                        }
                        html += '&nbsp;<a target="_blank" href="https://report.soupmaster.com.cn/WebReport/ReportServer?reportlet=doc/SpecialSubject/rpt_erp_poorder_exportbyorderid.cpt&orderNo='+full.orderNo+'&&format=pdf&__embed__=true">导出</a>';
                        return html;
                    }
                },
                {
                    "targets": [1],
                    "render": function(data, type, full) {
                        var html = '';
                        html += '<a href="/purchase-order/detail?orderNo=' + data + '">' + data + '</a>';
                        return html;
                    }
                },
                {
                    "targets": [2],
                    "orderable":false,
                    "searchable":false,
                    "render": function(data, type, full) {
                        if(data == '0') return '创建';
                        if(data == '1') return '已审核';
                        if(data == '2') return '已取消';
                        if(data == '9') return '已关闭';
                    }
                }
            ],
            order: [[ 1, "desc" ]]
        });

        $("#select_button").on('click', function () {
            table.ajax.reload();
        });

        $('#purchaseDate').datepicker({ language:'zh-CN', format:'yyyy-mm-dd'} );


        //重置操作
        $('#reset_button').click(function(){
            $('#orderNo').val('');
            $('#purchaseDate').val('');
            $("#orderStatus").val('');
            $('#warehouseId').val('');
            table.ajax.reload();
        });
    });



    //修改订单状态
    function editStatus(orderNo, status) {

        $.jBox.confirm("请确认操作", "提示", function(v){
            if (v == 'ok'){
                $.post('/purchase-order/edit-order-status', {orderNo:orderNo, status:status},
                    function (data) {
                        if(data.code == 200){
                            $.jBox.tip('操作成功', 'success');
                            table.draw(false);
                        }else{
                            $.jBox.error(data.val, '错误');
                        }
                    },
                    'json'
                );
            }else{
                $.jBox.close();
            }
        });
    }


    //自动审核
    function audit(orderNo) {
        $.jBox.confirm("确认审核通过吗?", "审核", function (v) {
            if (v == 'ok') {
                $.post('/purchase-order/audit-order', {orderNo: orderNo},
                    function (data) {
                        if (data.code == 200) {
                            $.jBox.tip('操作成功', 'success');
                            table.draw(false);
                        } else {
                            $.jBox.error(data.val, '错误');
                        }
                    },
                    'json'
                );
            }
        });
    }
</script>